// pages/coupon/coupon.js
var e = getApp(),
    t = e.requirejs("core"),
    a = e.requirejs("foxui"),
    i = e.requirejs("wxParse/wxParse"),
    totle = e.requirejs("foxui");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        /* 测试数据 arr*/
        arr: [],
        id: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var a = this;
        t.get("app/ewei_shopv2_api.php", {
            page:1,
            r: 'sale.coupon.getlist',
            cateid: ''
        }, function (t) {
            delete t.error;
            var e = t;
            a.setData({
                arr: e.list,
                show: true
            })
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    tab_fun: function (a) {
        console.log(this.data.items)
        var that = this;
        that.setData({
                show: false
            }),
            setTimeout(() => {
                var items = a.target.dataset
                if (items.id) {
                    that.setData({
                        show: true
                    })
                    that.receive(items)
                } else {
                    that.setData({
                        show: true
                    })
                    wx.showToast({
                        title: '领取失败请重试',
                        icon: 'none',
                        duration: 2000
                    })
                    that.onLoad()
                }
            }, 1000)
    },
    receive: function (a) {
        var items = a
        console.log(items)
        var e = this;

        var o = ` 确认使用${items.couponname}吗？`;
        t.confirm(o, function () {
            e.setData({
                buying: !0
            }), t.post("sale/coupon/pay", {
                id: items.id
            }, function (el) {
                if (el.error > 0) return a.toast(e, el.message), void s.setData({
                    buying: !1
                });
                t.get("sale/coupon/payresult", {
                    logid: el.logid
                }, function (i) {
                    if (i.error > 0) a.toast(e, i.message);
                    else if (0 != i.coupontype) {
                        var s = "/pages/sale/coupon/my/index/index";
                        1 == i.coupontype && (s = "/pages/member/recharge/index"), t.confirm(i.confirm_text, function () {
                            wx.redirectTo({
                                url: s
                            });
                        }, function () {
                            wx.redirectTo({
                                url: "/pages/sale/coupon/my/index/index"
                            });
                        });
                    } else wx.redirectTo({
                        url: "/pages/sale/coupon/my/showcoupons2/index?id=" + i.dataid
                    });
                });
            });
        });
    }
})